package leetcode.code1315;

import java.util.HashSet;
import java.util.Set;

import leetcode.IDebug;
import leetcode.helper.tree.TreeNode;

public class Solution implements IDebug {

	public int sumEvenGrandparent(TreeNode root) {
		Set<Integer> set = new HashSet<>();
		return this.find(root, 0, set);
	}

	private int find(TreeNode root, int level, Set<Integer> set) {
		int ans = 0;
		if (set.contains(level)) {
			ans += root.val;
		}
		if ((root.val & 1) == 0) {
			set.add(level + 2);
		}
		if (root.left != null) {
			ans += this.find(root.left, level + 1, set);
		}
		if (root.right != null) {
			ans += this.find(root.right, level + 1, set);
		}
		set.remove(level + 2);
		return ans;
	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
